/*
 * ===================================================
 *
 *       Filename:  fixed_point2.c
 *
 *
 *        Created:  10/20/2011 12:58:53 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  durand_y, <durand.yoan@gmail.com>
 *
 * ===================================================
 */
#include "fixed_point.h"

void    rec(int n)
{
  int   num = 0;
  char  c = '0';

  if (n == 0)
    return;
  if (n < 0)
    n = -n;
  if (n > 0)
    num = (n % 10) + c;
  rec(n / 10);
  write(STDOUT_FILENO, &num, 1);

}

void    print_int(int   n)
{
  char  c = '0';

  if (n < 0)
    write(STDOUT_FILENO, "-", 1);

  if (n == 0)
  {
    write(STDOUT_FILENO, &c, 1);
    return ;
  }
  rec(n);
}

int     int_to_fp(int   x)
{
  int   res = 0;

  res = x;
  res = res << 16;
  return (res);
}

void    fp_print(int    x)
{
  int   i = 0;
  float res = 0;

  i = x >> 16;
  print_int(i);
  write(STDOUT_FILENO, ".", 1);
  res = x * my_pow(2, -16) - i;
  res = res * 100000;
  if (res == 0)
    write(STDOUT_FILENO, "0000", 4);
  else
  {
    i = res;
    i = i % 10;
    if (i > 5)
      res = res + 10;
    res /= 10;
    print_int(res);
  }
  write(STDOUT_FILENO, "\n", 1);
}
